import 'package:get/get.dart';
import 'package:ty_smart_watch/bean/base_data_bean.dart';
import 'package:ty_smart_watch/bean/receive_data_bean.dart';
import 'package:ty_smart_watch/common/function_id.dart';
import 'package:ty_smart_watch/page/device/device_info_controller.dart';
import 'package:ty_smart_watch/page/smartlife/controller/body_grid_controller.dart';
import 'package:ty_smart_watch/page/sport/controller/sport_controller.dart';
import 'package:ty_smart_watch/page/watch_setting_page/controller/watch_data_controller.dart';

import '../page/smartlife/controller/today_step_controller.dart';
import '../utils/Log.dart';

/// 手表到App 数据解析类\
class UpdateBleToView {
  //region 单例模式
  static UpdateBleToView? _instance;

  factory UpdateBleToView() => _getInstance()!;

  static UpdateBleToView? get instance => _getInstance();

  static UpdateBleToView? _getInstance() {
    _instance ??= UpdateBleToView._internal();
    return _instance;
  }

  UpdateBleToView._internal() {
    _init();
  }

  _init() {}

  StepController stepController = Get.find();
  BodyGridController bodyGridController = Get.find();
  SportController sportController = Get.find();
  DeviceInfoController deviceInfoController = Get.find();

  updateToView(ReceiveDataBean bean) {
    Log.d("收到更新界面消息 >>  id: ${bean.functionId.toRadixString(16)}");
    switch (bean.functionId) {
      case STEP_TARGET:
        int target = bean.dataLongMap["stepTarget"] ?? 0;
        stepController.updateTargetStep(target.toString());
        break;
      case REAL_TIME_DATA:
        int stepData = bean.dataLongMap["stepData"] ?? 0;
        int calorie = bean.dataLongMap["calorie"] ?? 0;
        int distance = bean.dataLongMap["distance"] ?? 0;
        int heart = bean.dataLongMap["heart"] ?? 0;
        int battery = bean.dataLongMap["battery"] ?? 0;

        stepController.updateCurrStep(stepData.toString());
        stepController.updateCal((calorie / 1000).toStringAsFixed(1));
        stepController.updateDistance((distance / 1000).toStringAsFixed(2));
        stepController.updateHeart(heart.toString());
        bodyGridController.updateData(HEART, heart.toString());
        deviceInfoController.updateBattery(battery.toString());
        break;
      //运动数据
      case SPORT_RECORDER:
        var sportDataList = bean.sportDataList;
        if (sportDataList.isEmpty) {
          return;
        }
        sportDataList.sort((left, right) => right.timeStamp.compareTo(left.timeStamp));
        sportController.updateData(sportDataList);
        bodyGridController.updateTodaySportDistance(sportDataList);

        //独立设备页面
        try {
          WatchDataController watchDataController = Get.find();
          watchDataController.updateTodaySportDistance(sportDataList);
        } catch (e) {
          Log.d("$e");
        }
        break;

      case HEART:
        List<BaseDataBean>? heart = bean.dataBaseDataListMap["heart"];
        if (heart != null) {
          int heartData = heart.first.data;
          stepController.updateHeart(heartData.toString());
          bodyGridController.updateData(HEART, heartData.toString());

          //独立设备页面
          try {
            WatchDataController watchDataController = Get.find();
            watchDataController.updateData(HEART, heartData.toString());
          } catch (e) {
            Log.d("$e");
          }
        }
        break;
      case BLOOD_OXYGEN:
        List<BaseDataBean>? bloodOxygen = bean.dataBaseDataListMap["bloodOxygen"];
        if (bloodOxygen != null) {
          int bloodOxygenData = bloodOxygen.first.data;
          bodyGridController.updateData(BLOOD_OXYGEN, bloodOxygenData.toString());

          //独立设备页面
          try {
            WatchDataController watchDataController = Get.find();
            watchDataController.updateData(BLOOD_OXYGEN, bloodOxygenData.toString());
          } catch (e) {
            Log.d("$e");
          }
        }
        break;
      case BLOOD_PRESSURE:
        List<BaseDataBean>? bloodPressure = bean.dataBaseDataListMap["bloodPressure"];
        if (bloodPressure != null) {
          int bloodPressureData = bloodPressure.first.data;
          bodyGridController.updateData(BLOOD_PRESSURE, bloodPressureData.toString());

          //独立设备页面
          try {
            WatchDataController watchDataController = Get.find();
            watchDataController.updateData(BLOOD_PRESSURE, bloodPressureData.toString());
          } catch (e) {
            Log.d("$e");
          }
        }
        break;
      case G_SENSOR:
        int step = bean.dataLongMap["stepData"] ?? 0;
        int calorie = bean.dataLongMap["calorie"] ?? 0;
        int distance = bean.dataLongMap["distance"] ?? 0;
        stepController.updateCurrStep(step.toString());
        stepController.updateCal((calorie / 1000).toStringAsFixed(1));
        stepController.updateDistance((distance / 1000).toStringAsFixed(2));
        break;
    }
  }
}
